// 封装下 mjson.h，支持 get int 和 get long

#pragma once

#include <string>

#include "mjson.h"

// 仅在该文件内可见，其他文件内看不到
static const int MJSON_TO_STRING_BUF_SIZE = 8 * 1024;
static char MJSON_TO_STRING_BUF[MJSON_TO_STRING_BUF_SIZE];

int mjson_get_int(const char *s, int len, const char *path, int *v) {
    double val;
    int found = mjson_get_number(s, len, path, &val);
    // 虽然找不到时 val 为无用值，但这样转换并没有负面影响

    *v = (int)val;
    return found;
}

int mjson_get_long(const char *s, int len, const char *path, long *v) {
    double val;
    int found = mjson_get_number(s, len, path, &val);
    *v = (long)val;
    return found;
}

int mjson_get_bool(const char *s, int len, const char *path, bool *v) {
    int val;
    int found = mjson_get_bool(s, len, path, &val);
    *v = (bool)val;
    return found;
}

// 找到返回 1，找不到返回 0
int mjson_get_string(const char *s, int len, const char *path, std::string *out_str) {
    int found = mjson_get_string(s, len, path, MJSON_TO_STRING_BUF, MJSON_TO_STRING_BUF_SIZE);
    // 对于没找到和空字符串的情况都算做失败
    if (found <= 0) {
        return 0;
    }
    // assign 内部会做 copy，可放心使用
    out_str->assign(MJSON_TO_STRING_BUF);
    return 1;
}